$(function() {


	/**
	 * 请求头部，新增一行
	 */
	$('#cablemodel_add').click(function () {
		var html = $('#cablemodel_exmaple').html();
		$('#cable_model').append(html);
	});
	/**
	 * 请求头部，删除一行
	 */
	$('#cable_model').on('click', '.delete',function () {
		$(this).parents('.cable_model_item').remove();
	});


	/**
	 * 保存接口
	 */
	var addModalValidate = $("#ducomentForm").validate({
		errorElement : 'span',
		errorClass : 'help-block',
		focusInvalid : true,
		rules : {
			cablemodel : {
				required : true,
			},
			cablestandard : {
				required : true,
			}
		},
		messages : {
			cablemodel : {
				required :"请输入线缆型号",
			},
			cablestandard : {
				required :"请输入线缆规格",
			}
		},
		highlight : function(element) {
			$(element).closest('.form-group').addClass('has-error');
		},
		success : function(label) {
			label.closest('.form-group').removeClass('has-error');
			label.remove();
		},
		errorPlacement : function(error, element) {
			element.parent('div').append(error);
		},
		submitHandler : function(form) {

			// response params
			var cablestandardList = new Array();
			if ($('#cable_model').find('.cable_model_item').length > 0) {
			try { 
				$('#cable_model').find('.cable_model_item').each(function () {
					console.log($(this).find('.cablestandards').val());
					var name = $(this).find('.cablestandards').val();
					var tc_cablemodel = $("#cablemodel").val();
					if (name) {
						cablestandardList.push({
							'tc_cablemodel':tc_cablemodel,
							'tc_cablestandard':name
						});
					} else {
						throw "新增接口失败，录入内容不能为空！"; 
						return false;
					}
				});
			}catch (e) { 
				ComAlert.show(2, e);
				return false; 
			} 
			}
			var cablestandards = JSON.stringify(cablestandardList);
			// final params
			var params = $("#ducomentForm").serialize();
			params += '&' + $.param({
					'cablestandards':cablestandards,
			});

			$.post(base_url + "/cableinfo/add", params, function(data, status) {
				if (data.code == "200") {
					$('#addModal').modal('hide');
					setTimeout(function () {
						ComAlert.show(1, "新增成功", function(){
							window.location.href  = base_url + '/cableinfo';
						});
					}, 315);
				} else {
					ComAlert.show(2, (data.msg || "新增失败") );
				}
			});
		}
	});


});
